﻿package model.register;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import model.db.DBserver;
import model.login.LoginServer;
import model.message.MessageVo;

import org.jboss.netty.channel.Channel;
import org.json.simple.JSONObject;

import protocol.register.Register_protocol;
import util.KIntChange;

public class RegisterSever {
	private static RegisterSever registerSever;

	public static RegisterSever getInstance() {
		if (registerSever == null) {
			registerSever = new RegisterSever();
		}
		return registerSever;
	}

	public void regiterHandle( Register_protocol msg,Channel channe) throws IOException {

		String names = msg.getNames();
		String password =msg.getPassword();
		int sex = msg.getSex();

		Boolean result = regiters(names, password, sex);
		Boolean mapidResult = writeMapInfo(names);

		Register_protocol protocol = new Register_protocol();

		if (result) {
			protocol.setResult(1);
		} else {
			protocol.setResult(0);
		}

		channe.write(protocol.write());

	}

	private Boolean writeMapInfo(String names) {
		int id = LoginServer.getInstance().observerUserHas(names);
		if (id > 0) {
			HashMap map = new HashMap();
			map.put("mapid", 1);
			map.put("roleid", id);
			map.put("x", 568);
			map.put("y", 651);
			DBserver.getInstance().dbInsert("t_lastmap", map);
		}
		return false;
	}

	/**
	 * 
	 * 
	 * @param names
	 * @param password
	 */
	private Boolean regiters(String names, String password, int sex) {
		HashMap map = new HashMap();
		map.put("name", names);
		map.put("password", password);
		map.put("sex", sex);
		return DBserver.getInstance().dbInsert("t_user", map);
	}

	public void search() {
		String nameString = "kings";

		ArrayList fields = new ArrayList();
		fields.add("Name");
		Map lmap = new HashMap();
		String selCondition = " where Name =" + "'" + nameString + "'";
		DBserver.getInstance().dbSelect("tb_user ", fields, selCondition); // —>>>选择记录
	}

	private String geTimeString() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	}

	// public static void main(String[] args) {
	// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
	// 设置日期格式
	// System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
	// }

	// //end class
}
